package com.rzj.sys_lab_assistant.controller;

import com.rzj.sys_lab_assistant.entity.Assistant;
import com.rzj.sys_lab_assistant.entity.Technician;
import com.rzj.sys_lab_assistant.service.IAssistantService;
import com.rzj.sys_lab_assistant.service.ILabService;
import com.rzj.sys_lab_assistant.utils.FileNameUtils;
import com.rzj.sys_lab_assistant.utils.FileUtils;
import com.rzj.sys_lab_assistant.utils.ResultsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: sys_lab_assistant
 * @description:
 * @author: 作者
 * @create: 2021-06-29 19:58
 */
@Controller
@RequestMapping("assistant")
public class AssistantController {


    @Autowired
    IAssistantService assistantService;
    @Autowired
    ILabService labService;

    ResultsUtils resultsUtils;

    @RequestMapping("toAdd")
    public String toAdd(Model model) {
        model.addAttribute("lab", labService.listLabs());
        return "assistant/assistantAdd";
    }

    @RequestMapping("toEdit/{aId}")
    public String toEdit(Model model, @PathVariable("aId") int aId) {
        model.addAttribute("assistant", assistantService.getAssistantById(aId));
        return "assistant/assistantUpdate";
    }

    @RequestMapping("assistantList")
    public String assistantList(Model model) {
        model.addAttribute("assistantList", assistantService.listAssistants());
        //System.out.println("ass==>" + assistantService.listAssistants().toString());
        return "assistant/assistantList";
    }

    @RequestMapping("assistantByIdList")
    public String assistantByIdList(Model model, HttpSession session) { // 技术员界面
        Technician technician = (Technician) session.getAttribute("technician");
        model.addAttribute("assistantList", assistantService.listAssistantsById(technician.getAccount())); // session
        //System.out.println("ass==>" + assistantService.listAssistants().toString());
        return "assistantJob/assistantByIdList";
    }

    @RequestMapping("assistantAdd")
    @ResponseBody
    public Map<String, String> assistantAdd(@RequestParam("studentNo") String studentNo,
                                            @RequestParam("aName") String aName,
                                            @RequestParam("aPwd") String aPwd,
                                            @RequestParam("aMajor") String aMajor,
                                            @RequestParam("aCollege") String aCollege,
                                            @RequestParam("aGrade") String aGrade,
                                            @RequestParam("aEmail") String aEmail,
                                            @RequestParam("aMobile") String aMobile,
                                            @RequestParam("labId") String labId,
                                            @RequestParam("imgUrl") String imgUrl) {
        Assistant assistant = new Assistant(0, studentNo, aName, aPwd, aMajor, aCollege, aGrade, aEmail, aMobile, imgUrl, Integer.valueOf(labId), 0);
        int flag = assistantService.addAssistant(assistant);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "添加成功！");
        } else {
            return resultsUtils.resultsMap("error", "添加失败，请联系管理员！");
        }
    }

    @RequestMapping("assistantEdit")
    @ResponseBody
    public Map<String, String> assistantEdit(@RequestParam("aId") int aId,
                                             @RequestParam("studentNo") String studentNo,
                                             @RequestParam("aName") String aName,
                                             @RequestParam("aPwd") String aPwd,
                                             @RequestParam("aMajor") String aMajor,
                                             @RequestParam("aCollege") String aCollege,
                                             @RequestParam("aGrade") String aGrade,
                                             @RequestParam("aEmail") String aEmail,
                                             @RequestParam("aMobile") String aMobile,
                                             @RequestParam("labId") int labId,
                                             @RequestParam("imgUrl") String imgUrl,
                                             @RequestParam("status") int status) {
        Assistant assistant = new Assistant(0, studentNo, aName, aPwd, aMajor, aCollege, aGrade, aEmail, aMobile, imgUrl, labId, status);
        int flag = assistantService.editAssistant(assistant);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "修改成功！");
        } else {
            return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
        }
    }

    @RequestMapping("assistantDel")
    @ResponseBody
    public Map<String, String> assistantDel(@RequestParam("aId") String aId) {
        int flag = assistantService.delAssistant(Integer.valueOf(aId));
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "删除成功！");
        } else {
            return resultsUtils.resultsMap("error", "删除失败，请联系管理员！");
        }
    }

    @RequestMapping("assistantChange")
    @ResponseBody
    public Map<String, String> assistantChange(@RequestParam("aId") String aId) {
        int flag = assistantService.editAssistantByStatus(Integer.valueOf(aId));
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "离职成功！");
        } else {
            return resultsUtils.resultsMap("error", "离职失败，请联系管理员！");
        }
    }

    /**
     * @param file 要上传的文件
     * @return
     */
    @RequestMapping("fileUpload")
    @ResponseBody
    public Map<String, String> upload(@RequestParam(value = "file") MultipartFile file) {

        Map<String, String> map = new HashMap<>();
        // 要上传的目标文件存放路径
        String localPath = "E:/Code_workerpalce/sys_lab_assistant/src/main/resources/static/img";
        String sqlPath = "http://localhost:9527/sysLab/img/";
        // 上传成功或者失败的提示
        String msg = "";
        String data = "";

        //System.out.println("file==>" + file.getOriginalFilename());

        String fileName = FileNameUtils.getFileName(file.getOriginalFilename());
        String fileUrl = sqlPath + fileName;
        //System.out.println("fileUrl==>" + fileUrl);

        //System.out.println("fileName==>" + fileName);

        if (FileUtils.upload(file, localPath, fileName)) {
            // 上传成功，给出页面提示
            data = "success";
            msg = "上传成功！";
            map.put("fileUrl", fileUrl);
        } else {
            data = "error";
            msg = "上传失败！";

        }

        // 显示图片
        map.put("data", data);
        map.put("msg", msg);

        //map.put("fileName", file.getOriginalFilename());

        return map;
    }



}
